const Koa = require("koa");
const KoaRouter = require("@koa/router");

// 创建app对象
const app = new Koa();

// 注册路由对象
const userRouter = new KoaRouter({ prefix: "/users" });

userRouter.get("/", (ctx, next) => {
    // 1.body的类型是string
    // ctx.body = 'user list data~'

    // 2.body的类型是Buffer
    // ctx.body = Buffer.from('你好啊, 李银河~')

    // 3.body的类型是Stream
    // const readStream = fs.createReadStream('./uploads/1668331072032_kobe02.png')
    // ctx.type = 'image/jpeg'
    // ctx.body = readStream

    // 4.body的类型是数据(array/object) => 使用最多
    ctx.status = 201;
    ctx.body = {
        code: 0,
        data: [
            { id: 111, name: "iphone", price: 100 },
            { id: 112, name: "xiaomi", price: 990 },
        ],
    };

    // 5.body的值是null, 自动设置http status code为204
    // ctx.body = null
});

app.use(userRouter.routes());
app.use(userRouter.allowedMethods());

// 启动服务器
app.listen(6000, () => {
    console.log("koa服务器启动成功~");
});
